package com.freight.core.serviceimpl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freight.core.common.utils.IpInfoUtil;
import com.freight.core.common.utils.SecurityUtil;
import com.freight.core.dao.OperationLogDao;
import com.freight.core.dao.mapper.OperationLogMapper;
import com.freight.core.entity.OperationLog;
import com.freight.core.entity.User;
import com.freight.core.service.OperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OperationLogServiceImpl implements OperationLogService {

    @Autowired
    private OperationLogDao operationLogDao;

    @Autowired
    SecurityUtil securityUtil;

    @Autowired
    private IpInfoUtil ipInfoUtil;

    @Autowired(required = false)
    private HttpServletRequest request;

    @Autowired
    private OperationLogMapper operationLogMapper;

    @Override
    public Page<OperationLog> getOperationLogList(Page<OperationLog> page, Integer logType, Integer oid) {

        return operationLogMapper.getOperationLogList(page,logType,oid);
    }

    @Override
    public void saveUserOperationLog(User user,Integer logType, String name, String operationData) {
        OperationLog operationLog = new OperationLog();
        operationLog.setUserName(user.getUsername());
        operationLog.setNickName(user.getNickName());
        operationLog.setUserId(user.getId());
        operationLog.setIp(ipInfoUtil.getIpAddr(request));

        operationLog.setLogType(logType);
        operationLog.setName(name);
        operationLog.setOperationData(operationData);

        operationLogDao.save(operationLog);

    }

    @Override
    public void saveOperationLog(Integer logType,String name,String operationData) {
        User user = securityUtil.getCurrUser();

        OperationLog operationLog = new OperationLog();
        operationLog.setUserName(user.getUsername());
        operationLog.setNickName(user.getNickName());
        operationLog.setUserId(user.getId());
        operationLog.setIp(ipInfoUtil.getIpAddr(request));

        operationLog.setLogType(logType);
        operationLog.setName(name);
        operationLog.setOperationData(operationData);

        operationLogDao.save(operationLog);

    }
}
